<?php

/*
 * +----------------------------------------------------------------------
 * 作 者: songyb <1371626452@qq.com>
 * +----------------------------------------------------------------------
 * 创建日期：2014-03-08
 * +----------------------------------------------------------------------
 * 文件描述：教师研修网用户登录
 * +----------------------------------------------------------------------
 * 升级记录：
 * +----------------------------------------------------------------------
 */
class IndexAction extends CommonAction
{

    public function index()
    {
        redirect(__APP__ . '?g=Index&m=Index&a=login');
    }

    public function login()
    {
        $this->display();
    }

    /**
     * 检查用户登录信息
     */
    public function check_login()
    {
        $name = $_REQUEST['name'];
        $pwd = $_REQUEST['pwd'];
        if (empty($name)) {
            $this->error("用户名不能为空");
        }
        if (empty($pwd)) {
            $this->error("密码不能为空");
        }
        $model = new GroupsUserModel();
        // 查询当前用户名是否存在
        $temp = $model->where("name = '" . $name . "'")->getField('id');
        $password = $model->where("pwd = '" . $pwd . "'")->getfield('id');
        if (empty($temp) || ($password)) {
            $this->error('您输入的用户名或密码不存在，请检查！');
        }
        
        // 查询用户名和密码是否匹配
        $user = $model->field("id, name, real_name, type, end_time, feedback, page")
            ->where("name = '" . $name . "'
					  	   and pwd = '" . md5($pwd) . "'")
            ->find();
        if (! empty($user)) {
            // 查询该用户对应问卷
            // $form_data = $model->table('form a,
            // groups_user b,
            // groups_form c')
            // ->where("a.id = c.formid
            // and b.groupsid = c.groupsid
            // and b.id = '".$user['id']."'")
            // ->getField('a.id');
            // if(empty($form_data)) {
            // $this->assign('jumpUrl', __APP__.'?g=Login&m=Index&a=index');
            // $this->error('没有找到您的问卷，请与管理员联系！');
            // }
            $formid = get_system_option("问卷" . $user['type']);
            //dump($user['type']);
            if (empty($formid)) {
                $this->error('非法的用户类型！');
            }
            // 验证该用户是否已经提交问卷
            if (! empty($user['end_time']) && $user['page'] == - 1) {
                if (empty($user['feedback'])) {
                    cookie('userid', $user['id']);
                    cookie('user_type', $user['type']);
                    cookie('user_name', $user['name']);
                    cookie('user_real_name', $user['real_name']);
                    $this->display("feedback");
                    exit();
                } else {
                	if($user['type']=='b'){
                		$this->assign("jumpUrl", __APP__ . '?g=Index&m=Index&a=login_role&type='.$user['type']);
                		$this->success('登录成功，正在跳转至被测教师选择页面！');
                	}else{
                		$this->display("finish");
                		exit();
                	}
                }
                // $this->assign('jumpUrl', __APP__.'?g=Login&m=Index&a=index');
                // $this->error('您已提交问卷，不能再次提交！');
            }
            cookie('userid', $user['id']);
            cookie('user_type', $user['type']);
            cookie('user_name', $user['name']);
            cookie('user_real_name', $user['real_name']);
            $this->assign('jumpUrl', __APP__ . '?g=Index&m=Index&a=login_role&formid=' . $formid."&type=".$user['type']);
           // $this->assign('jumpUrl', __APP__ . '?g=Index&m=Form&a=view&formid=' . $formid . '&ops=save&page=' . $user['page']);
            $this->success('登录成功，正在跳转至被测教师选择页面！');
        } else {
            $this->assign('jumpUrl', __APP__ . '?g=Index&m=Index&a=login');
            $this->error('登录失败，用户名或密码不正确！');
        }
    }

    public function logout()
    {
        cookie('userid', null);
        cookie('user_name', null);
        cookie('user_real_name', null);
        $this->assign('jumpUrl', __APP__ . '?g=Index&m=Index&a=login');
        $this->success('退出成功，您可以登录系统继续填答测评问卷！');
    }

    /**
     * 显示结束界面
     */
    public function finish()
    {
    	//获取当前用户类型
    	$userid = cookie('userid');
    	dump($userid);
    	$model = new GroupsUserModel();
    	$user_list = $model->where("id='".$userid."'")->find();
    	$this->assign('type', $user_list['type']); 
        $this->display();
    }

    /**
     * 生成验证码
     */
    public function verify()
    {
        header("Content-type: text/html; charset=utf-8");
        import("ORG.Util.Image");
        Image::buildImageVerify(4, 1, 'png', 130, 40);
    }

    /**
     * 学生注册页面显示
     */
    public function reg_student()
    {
        $this->assign('province_list', $this->get_province_list()); // 省份列表
        
        $this->assign('gender_list', set_field_dict_list('user', 'gender'));
        $this->generate_birthday_year(); // 出生年月-年
        $this->generate_birthday_month(); // 出生年月-月
        $this->generate_birthday_day(); // 出生年月-日
        $this->generate_class(); // 班级
        //查询学院
        $college_model = new CollegeModel();
        $colledge_data = $college_model->field("id,name")->select();
        $this->assign('colledge_data', $colledge_data); 
        $this->display();
    }

    /**
     * 学生注册信息存储
     */
    public function student_save()
    {
        $model = new GroupsUserModel();
        
        $default_groupsid = get_system_option("默认分组编号");
        $data = $model->create();
        $default_groupsid = get_system_option("学生组");
        $default_formid = $this->get_formid_by_groupid($default_groupsid);
        $data['groupsid'] = $default_groupsid;
        $data['name'] = $_POST['real_name'];
        $data['pwd'] = md5($_POST['pwd']);
        $data['birth'] = $_POST['birth_year'] . "-" . $_POST['birth_month'];
        $data['operator'] = '1';
        $data['type'] = 'a'; // 学生类型
        $id = $model->add($data);
        if (false !== $id) {
            cookie("userid", $id);
            cookie("user_name", $data['name']);
            cookie("user_real_name", $data['real_name']);
            cookie("user_type", 'a');
            $this->assign('jumpUrl', __APP__ . '?g=Index&m=Index&a=login_role&formid=' . $default_formid."&type=".$data['type']);
            $this->success("注册成功，请填选择被测评教师");
            //$this->success("注册成功，请填写测评问卷", __APP__ . '?g=Index&m=Form&a=view&formid='.$default_formid);
        } else {
            $this->error("注册失败");
        }
    }

    /**
     * 教师注册页面显示
     */
    public function reg_teacher()
    {
        $this->assign('province_list', $this->get_province_list()); // 省份列表
        $this->assign('gender_list', set_field_dict_list('user', 'gender'));
        $this->generate_birthday_year(); // 出生年月-年
        $this->generate_birthday_month(); // 出生年月-月
        $this->generate_birthday_day(); // 出生年月-日
        $this->generate_class(); // 班级
        
        $model = new ProvinceModel();
        // $data = $model->field("code, name")->where("code in ('62','36','42','53','33')")->select();
        
        $data = $model->field("code, name")
            ->where("code in (select substring(area_code, 1, 2) from school)")
            ->select();
        //查询学院
        $college_model = new CollegeModel();
        $colledge_data = $college_model->field("id,name")->select();
        $this->assign('colledge_data', $colledge_data); 
        $this->assign('profession_data', set_field_dict_list('teacher', 'type')); 
        $this->display();
    }

    /**
     * 教师注册信息存储
     */
    public function teacher_save()
    {
        $model = new GroupsUserModel();
        
        //$default_groupsid = get_system_option("默认分组编号");
        //$default_formid = $this->get_formid_by_groupid($default_groupsid);
        $data = $model->create();
        $data['name'] = $_POST['real_name'];
        $data['pwd'] = md5($_POST['pwd']);
        $data['birth'] = $_POST['birth_year'] . "-" . $_POST['birth_month'];
        $data['class'] = implode(',', $_POST['class']);
        $data['operator'] = '1';
        //判断类型
        if($_POST['leader_status']=='a'){
        	$data['type'] = 'c'; // 领导类型
        	$default_groupsid = get_system_option("领导组");
        	$default_formid = $this->get_formid_by_groupid($default_groupsid);
        	$data['groupsid'] = $default_groupsid;
        }else{
        	$data['type'] = 'b'; // 教师类型
        	$default_groupsid = get_system_option("教师组");
        	$default_formid = $this->get_formid_by_groupid($default_groupsid);
        	$data['groupsid'] = $default_groupsid;
        }
        //保存后台
        $user_model = new UserModel();
        $user_data['name'] = $data['name'];
        $user_data['pwd'] = $data['pwd'];
        $user_data['real_name'] = $data['real_name'];
        $user_data['gender'] = $data['gender'];
        $user_data['type'] = $data['type'];
        $user_data['phone'] = $data['phone'];
        $user_data['area_code'] = $data['area_code'];
        $user_data['status'] = 'a';
        $user_data['update_date'] = date("Y-m-d");
        $user_model->add($user_data);
        $id = $model->add($data);
        if (false !== $id) {
            cookie("userid", $id);
            cookie("user_name", $data['name']);
            cookie("user_real_name", $data['real_name']);
            cookie("user_type", 'b');
            $this->assign('jumpUrl', __APP__ . '?g=Index&m=Index&a=login_role&formid=' . $default_formid."&type=".$data['type']);
            $this->success("注册成功，请填选择被测评教师");
           // $this->success("注册成功，请填写问卷", __APP__.'?g=Index&m=Form&a=view&formid='.$default_formid);
        } else {
            $this->error("注册失败");
        }
    }

    /**
     * 校长注册页面显示
     */
    public function reg_master()
    {
        $this->assign('province_list', $this->get_province_list()); // 省份列表
        $this->assign('gender_list', set_field_dict_list('user', 'gender'));
        $this->generate_birthday_year(); // 出生年月-年
        $this->generate_birthday_month(); // 出生年月-月
        $this->generate_birthday_day(); // 出生年月-日
        $this->display();
    }

    /**
     * 校长注册信息存储
     */
    public function master_save()
    {
        $model = new GroupsUserModel();
        
        $default_groupsid = get_system_option("默认分组编号");
        $data = $model->create();
        $data['groupsid'] = $default_groupsid;
        $data['name'] = $_POST['real_name'];
        $data['pwd'] = md5($_POST['pwd']);
        $data['operator'] = '1';
        $data['type'] = 'c'; // 校长类型
        $id = $model->add($data);
        if (false !== $id) {
            cookie("userid", $id);
            cookie("user_name", $data['name']);
            cookie("user_real_name", $data['real_name']);
            cookie("user_type", 'c');
            $this->success("注册成功，请填写测评问卷", __APP__ . '?g=Index&m=Form&a=view&formid=cms_def_webid_20160909175705_ohxp09aab9w');
        } else {
            $this->error("注册失败");
        }
    }

    /**
     * 局长注册页面显示
     */
    public function reg_director()
    {
        $this->assign('province_list', $this->get_province_list()); // 省份列表
        $this->assign('gender_list', set_field_dict_list('user', 'gender'));
        $this->generate_birthday_year(); // 出生年月-年
        $this->generate_birthday_month(); // 出生年月-月
        $this->generate_birthday_day(); // 出生年月-日
        $this->display();
    }

    /**
     * 局长注册信息存储
     */
    public function director_save()
    {
        $model = new GroupsUserModel();
        
        $default_groupsid = get_system_option("默认分组编号");
        $data = $model->create();
        $data['groupsid'] = $default_groupsid;
        $data['name'] = $_POST['real_name'];
        $data['pwd'] = md5($_POST['pwd']);
        $data['operator'] = '1';
        $data['type'] = 'd'; // 县管类型
        $id = $model->add($data);
        if (false !== $id) {
            cookie("userid", $id);
            cookie("user_name", $data['name']);
            cookie("user_real_name", $data['real_name']);
            cookie("user_type", 'd');
            $this->success("注册成功，请填写测评问卷", __APP__ . '?g=Index&m=Form&a=view&formid=cms_def_webid_20160909155542_dl5ucsahwxs');
        } else {
            $this->error("注册失败");
        }
    }

    /**
     * 反馈页面
     */
    public function feedback()
    {
        $this->display();
    }

    /**
     * 反馈页面
     */
    public function feedback_save()
    {
        $userid = cookie('userid');
        $feedback = $_REQUEST['feedback'];
        $model = new GroupsUserModel();
        if (false !== $model->where("id = '" . $userid . "'")->setField('feedback', $feedback)) {
            $this->success("问卷反馈提交成功，谢谢您的合作，祝您生活愉快。", __APP__ . "?g=Index&m=Index&a=finish");
        } else {
            $this->success("反馈提交失败。", __APP__ . "?g=Index&a=Index&m=feedback");
        }
        $this->display();
    }

    /**
     * 学生帮助界面
     */
    public function help_student()
    {
        $this->display();
    }

    /**
     * 教师帮助界面
     */
    public function help_teacher()
    {
        $this->display();
    }

    /**
     * 校管帮助界面
     */
    public function help_master()
    {
        $this->display();
    }

    /**
     * 县管帮助界面
     */
    public function help_director()
    {
        $this->display();
    }

    /**
     * 生成出生年月-年
     */
    public function generate_birthday_year()
    {
        $year = array();
        for ($i = 1950; $i < 2016; $i ++) {
            $year[$i]["id"] = $i;
            $year[$i]["name"] = $i . "年";
        }
        $this->assign('year_list', $year);
    }

    /**
     * 生成出生年月-月
     */
    public function generate_birthday_month()
    {
        $month = array();
        for ($i = 1; $i <= 9; $i ++) {
            $month[$i]["id"] = "0" . $i;
            $month[$i]["name"] = "0" . $i . "月";
        }
        for ($i = 10; $i <= 12; $i ++) {
            $month[$i]["id"] = $i;
            $month[$i]["name"] = $i . "月";
        }
        $this->assign('month_list', $month);
    }

    /**
     * 生成出生年月-日
     */
    public function generate_birthday_day()
    {
        $day = array();
        for ($i = 1; $i <= 31; $i ++) {
            $day[$i]["id"] = $i;
            $day[$i]["name"] = $i . "日";
        }
        $this->assign('day_list', $day);
    }

    /**
     * 生成班级
     */
    public function generate_class()
    {
        $class = array();
        for ($i = 1; $i <= 20; $i ++) {
            $class[$i]["id"] = $i;
            $class[$i]["name"] = "（" . $i . "）班";
        }
        $this->assign('class_list', $class);
    }
    public function forgot_pwd(){
    	$this->display();
    }
    public function update_pwd(){
    	$model = new GroupsUserModel();
    	//查询用户是否存在
    	$user_list = $model->where("name='".$_POST['real_name']."'")->find();
    	if (empty($user_list)) {
    		$this->error("真实姓名错误，请重新输入。", __APP__ . "?g=Index&m=Index&a=forgot_pwd");
    	}else{
    		if (false == $model->where("id = '" . $user_list['id']. "'")->setField('pwd', md5($_POST['pwd']))) {
    			$this->error("密码修改失败。", __APP__ . "?g=Index&m=Index&a=forgot_pwd");
    		}else{
    			$this->success("密码修改成功。", __APP__ . "?g=Index&m=Index&a=login");
    		}
    	}
    }
    public function login_role(){
    	$userid = cookie('userid');
    	$this->assign('userid', $userid);
    	$formid = $this->_request("formid");
    	$type = $this->_request("type");
    	$this->assign('formid', $formid);
    	$this->assign('type', $type);
    	$this->assign('province_list', $this->get_province_list()); // 省份列表
    	$this->assign('gender_list', set_field_dict_list('user', 'gender'));
    	$this->generate_birthday_year(); // 出生年月-年
    	$this->generate_birthday_month(); // 出生年月-月
    	$this->generate_birthday_day(); // 出生年月-日
    	$this->generate_class(); // 班级
    	
    	$model = new ProvinceModel();
    	// $data = $model->field("code, name")->where("code in ('62','36','42','53','33')")->select();
    	
    	$data = $model->field("code, name")
    	->where("code in (select substring(area_code, 1, 2) from school)")
    	->select();
    	//查询学院
    	$college_model = new CollegeModel();
    	$colledge_data = $college_model->field("id,name")->select();
    	$this->assign('colledge_data', $colledge_data); 
    	$this->display();
    }
    public function login_role_form(){
    	$model = new GroupsUserModel();
    	$teacher =  $this->_request("teacher");
    	$userid = cookie('userid');
    	$user = $model->where("id='".$userid."'")->find();
    	$type =  $this->_request("type");
    	if($type=='b'){
    		//判断是否自评
    		if($user['id']==$teacher){
    			$formid = $this->_request("formid");
    			if (! empty($user['end_time']) && $user['page'] == - 1) {
    				$this->display("finish");
    				exit();
    			}else{
    				//判断是否语音测评
    				$media_model = new MediaModel();
    				$res= $media_model->where("operator = '".$userid."'")->find();
    				//将被测教师添加至group_user
    				$model->where("id = '".$userid."'")->setField("bcjsid",$teacher);
    				//进行语言测评
    				if(empty($res)){
    					$this->assign('jumpUrl', __APP__ . '?g=Index&m=SpeechRecognition&a=index&formid=' . $formid);
    					$this->success('被测评教师选择成功，正在跳转至测评页面！');
    				}
    			}
    		}else{
    			
    			$default_groupsid = get_system_option("同事组");
    			$formid = $this->get_formid_by_groupid($default_groupsid);
    			//将被测教师添加至group_user
    			$model->where("id = '".$userid."'")->setField("bcjsid",$teacher);
    		}
    	}elseif($type=='c'){
    		$default_groupsid = get_system_option("领导组");
    		$formid = $this->get_formid_by_groupid($default_groupsid);
    		//将被测教师添加至group_user
    		$model->where("id = '".$userid."'")->setField("bcjsid",$teacher);
    	}else{
    		$default_groupsid = get_system_option("学生组");
    		$formid = $this->get_formid_by_groupid($default_groupsid);
    		//将被测教师添加至group_user
    		$model->where("id = '".$userid."'")->setField("bcjsid",$teacher);
    	}
    	//dump($role);
    	$this->assign('jumpUrl', __APP__ . '?g=Index&m=Form&a=view&formid=' . $formid . '&ops=save&page=' . $user['page']);
    	$this->success('被测评教师选择成功，正在跳转至测评页面！');
    }
    public function voice_cp(){
    	$this->display();
    }
}
?>